package com.jdk.learn.thread;

public class ThreadStop {
    public static void main(String[] args) throws InterruptedException {
        StopDemo stopDemo=new StopDemo();
        stopDemo.start();
        for (int i = 0; i < 5; i++) {
            System.out.println("主线程开始："+i);
            Thread.sleep(1000);
            if (i==2) {
//                stopDemo.stop();
//                stopDemo.stopThread(); //此时线程不终止运行 还在执行
                stopDemo.interrupt();
            }

        }
    }
}

class StopDemo extends Thread {
    private volatile boolean flag=true;
    @Override
    public synchronized void run() {
        System.out.println("子线程开始");
        while (flag){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
                stopThread();
            }

        }
        System.out.println("subend");

    }
    public void stopThread(){
        this.flag=false;
    }
}